package com.yunyang.jczd.facade.fileexport.controller;

import com.yunyang.jczd.base.file.fileexport.dto.JczdExportRecordDTO;
import com.yunyang.jczd.business.response.Response;
import com.yunyang.jczd.business.response.ResponseUtils;
import com.yunyang.jczd.business.web.current.RequestContext;
import com.yunyang.jczd.facade.fileexport.entity.ExportConfig;
import com.yunyang.jczd.facade.fileexport.enums.ExportRequestMethod;
import com.yunyang.jczd.facade.fileexport.mapper.ExportConfigMapper;
import com.yunyang.jczd.facade.fileexport.service.IExportService;
import com.yunyang.jczd.web.common.JczdHeaders;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 文件导出
 *
 * @author xiaohei
 * @create 2020-12-17 上午10:37
 **/
@Api(tags = "文件导出 - API")
@RestController
@RequestMapping("/facade/export")
@AllArgsConstructor
public class ExportController {

    private ExportConfigMapper configMapper;

    private IExportService exportService;

    private Map<Integer, Map<String, String>> cacheHeader = new HashMap<>(50);

    @ApiOperation(value = "导出")
    @PostMapping(value = "/{exportId}")
    public Response<JczdExportRecordDTO> saveExport(HttpServletRequest request, @RequestBody Map<String, Object> param, @PathVariable String exportId) {
        ExportConfig exportConfig = configMapper.selectById(exportId);
        HttpHeaders headers = buildHeader(exportConfig);
        //分页只查询一条数据
        param.put("size",1);
        Response response = exportService.tryRequest(headers, param, exportConfig);
        if (!ResponseUtils.isSuccess(response)) {
            return ResponseUtils.fail(response.getCode(), response.getMessage());
        }
        exportService.export(headers, param, exportConfig);
        return ResponseUtils.success();
    }

    private HttpHeaders buildHeader(ExportConfig exportConfig) {
        HttpHeaders headers = new HttpHeaders();
        //这个其实
        Map<String, String> map = getCacheHeader(exportConfig);
        for (Map.Entry<String, String> entry : map.entrySet()) {
            headers.set(entry.getKey(), entry.getValue());
        }
        if (RequestContext.getTenantId() != null) {
            headers.set(JczdHeaders.TENANT_ID, String.valueOf(RequestContext.getTenantId()));
        }
        return headers;
    }

    private Map<String, String> getCacheHeader(ExportConfig exportConfig) {
        if (!cacheHeader.containsKey(exportConfig.getId())) {
            Map<String, String> header = new HashMap<>();
            String headerStr = exportConfig.getDataRequestHeader();
            if (StringUtils.isNotEmpty(headerStr)) {
                String[] stirs = StringUtils.split(headerStr, "&");
                for (String str : stirs) {
                    if (StringUtils.contains(str, "=")) {
                        header.put(StringUtils.substringBefore(str, "="), StringUtils.substringAfter(str, "="));
                    }
                }
            }
            cacheHeader.put(exportConfig.getId(), header);
            return header;
        }
        return cacheHeader.get(exportConfig.getId());


    }
}
